package com.fjnu.controller.shiro;

import java.util.List;
import java.util.Map;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fjnu.controller.common.BaseController;
import com.fjnu.entities.Permission;
import com.fjnu.service.IPermissionService;
@Controller
public class PermissionController extends BaseController{
	private static Logger logger =LogManager.getLogger(PermissionController.class);
	@Autowired
	private IPermissionService permissionService;
	@ResponseBody
	@RequestMapping(value="/addPermission")
	public Map<String,Object> addPermission(@RequestBody Permission permission){
		logger.info("新增权限");
		if(!init(permission).get(STATUS).equals(SUCCESS))
			return getData2Result();
		permissionService.addPermission(permission);
		return getData2Result();
	}
	
	@ResponseBody
	@RequestMapping(value="/delPermission")
	public Map<String,Object> delPermission(int id){
		logger.info("删除权限");
		if(!init(id).get(STATUS).equals(SUCCESS))
			return getData2Result();
		
		try {
			permissionService.delPermission(id);
		} catch (Exception e) {
			putError("该权限已被占用，请先删除");
		}
		return getData2Result();
	}
	@ResponseBody
	@RequestMapping(value="/findAllPermission")
	public Map<String,Object> findAllPermission(){
		logger.info("查询所有权限");
		if(!init().get(STATUS).equals(SUCCESS))
			return getData2Result();
		List<Permission> list=permissionService.findAllPermission();
		put("data",list);
		return getData2Result();
	}
}
